package xc.com.oss.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import xc.com.oss.io.EqualsUtil;
import xc.com.oss.service.FileService;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Description TODO
 * @Author yc
 * @Date 2021/5/28 11:46
 **/
@Service
public class FileServiceImpl implements FileService {

    private static final String bucketName = "zlzimg";
    private static final String PREFIX = "xuecheng/";
    @Autowired
    private OSS ossClient;

    /**
     * 上传文件
     *
     * @param file
     * @return
     */
    @Override
    public String upload(MultipartFile file) {
        PutObjectResult result = null;
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());

        String fileBaseName = UUID.randomUUID() + ".";
        String fileName = fileBaseName + FilenameUtils.getExtension(file.getOriginalFilename());
        //设置图片在线浏览
        ObjectMetadata objectMetadata=new ObjectMetadata();
        //设置图片在线浏览类型
        objectMetadata.setContentType(extension);
        try {
            result = ossClient.putObject(
                    bucketName,  PREFIX + EqualsUtil.strExtension(extension)+fileName
                    , file.getInputStream(),objectMetadata);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String url = "https://zlzimg.oss-cn-shenzhen.aliyuncs.com/"+PREFIX + EqualsUtil.strExtension(extension)+fileName;
        return url;
    }

    @Override
    public Map<String, Object>uploadMedia(MultipartFile file) {
        PutObjectResult result = null;
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        try {
            InputStream stream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        String fileBaseName = UUID.randomUUID() + ".";
        String fileName = fileBaseName + FilenameUtils.getExtension(file.getOriginalFilename());
        //设置图片在线浏览
        ObjectMetadata objectMetadata=new ObjectMetadata();
        //设置图片在线浏览类型
        objectMetadata.setContentType(extension);
        try {
            result = ossClient.putObject(
                    bucketName,  PREFIX + EqualsUtil.strExtension(extension)+fileName
                    , file.getInputStream(),objectMetadata);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String, Object> map=new HashMap<>();
        map.put("file_path","https://zlzimg.oss-cn-shenzhen.aliyuncs.com/"+PREFIX + EqualsUtil.strExtension(extension));
        map.put("file_url",fileName);
        String url = "https://zlzimg.oss-cn-shenzhen.aliyuncs.com/"+PREFIX + EqualsUtil.strExtension(extension)+fileName;
        return map;
    }
    @Override
    public boolean delete(String fileName) {
        String extension = FileNameUtil.extName(fileName);
        String key = PREFIX+EqualsUtil.strExtension(extension) + fileName;
        ossClient.deleteObject(bucketName, key);
        return true;
    }
}
